<?php
/**
 * created by zzy
 * date: 2018/1/26 14:07
 */


namespace App\Api\Service\Waste\Controllers;


use App\Eloquent\Ygt\DepartmentUser;
use App\Eloquent\Ygt\ImgUpload;
use App\Eloquent\Ygt\Product;
use App\Eloquent\Ygt\Purchase;
use App\Eloquent\Ygt\ReturnPurchase;
use App\Eloquent\Ygt\ReturnPurchaseMaterial;
use App\Eloquent\Ygt\SellerBill;
use App\Eloquent\Ygt\SellerCompany;
use App\Eloquent\Ygt\WasteBill;
use App\Engine\Func;
use Framework\BaseClass\Api\Controller;

class BillController extends Controller
{
    public $userId        = 25;
    public function index(){}
    public function lists()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $page               = request('page',1);
        $limit              = request('limit',10);
        $id                 = request('id',10);
        if(!$userInfo || !$id)
        {
            xThrow( ERR_PARAMETER,'登录超时' );
        }
        $companyId          = $userInfo->company_id;
        $where              = [
            'ygt_waste_bill.company_id'=>$companyId,
            'ygt_waste_bill.waste_buyer_id'=>$id,
        ];
        $offset             = ($page-1) * $limit;
        $lists              = WasteBill::getJoinList($where,$limit,$offset)->toArray();
        $count              = WasteBill::getCount($where);
        $listsNew           = [];
        foreach($lists as $key=>$val)
        {
            $temp           = [
                'id'=>$val['id'],
                'sn'=>'编号: '.$val['sn'],
                'total_money'=>'¥'.$val['total_money'],
                'now_money'=>'¥'.$val['now_money'],
                'pay_type'=>'付款方式: '.$val['pay_type'],
            ];
            $valImgUrl      = $val['img_url'];
            $imgPath        = '';
            if($valImgUrl)
            {
                $imgPath    = Func::getImgUrlHttp($valImgUrl);
            }
            $temp['img_path']   = $imgPath;

            $valCreatedAt   = $val['created_at'];
            $createdAtYmd   = date('Y-m-d',$valCreatedAt);
            $temp['created_at_ymd'] = '生成日期: '.$createdAtYmd;

            $listsNew[]     = $temp;
        }
        $pagingData         = [
            'page' => $page,
            'pageSize' => $limit,
            'total' => $count,
            'keyword' => ''
        ];
        return $this->pagingData($listsNew, $pagingData);
    }
    public function edit()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        if(!$userInfo)
        {
            xThrow( ERR_PARAMETER,'登录超时' );
        }
        $companyId          = $userInfo->company_id;
        $requestData        = request()->all();
        $rules              = [
            'waste_buyer_id'=> 'required',
            'money'=>'required',
            'pay_type'=>'required',
        ];
        $rulesMessage       = [
            'waste_buyer_id.required'=> '收品单位',
            'money.required'=> '金额',
            'pay_type.required'=> '付款方式',
        ];
        $validator          = \Validator::make($requestData,$rules,$rulesMessage);
        $validatorFails     = $validator->fails();
        if ($validatorFails)
        {
            $rulesError     = $validator->errors()->toArray();
            foreach ($rulesError as $key=>$val)
            {
                xThrow( ERR_PARAMETER,$val[0]);
            }
        }

        $data                   = $requestData;
        $money                  = $requestData['money'];
        //其它信息
        $sn                      = 'SP'.$_SERVER['REQUEST_TIME'].$userId.mt_rand(1000,9999);
        $data['sn']              = $sn;
        $data['company_id']     = $companyId;
        $data['total_money']    = $money;
        $data['now_money']      = $money;
        $data['add_user_id']    = $userId;

        $fieldList              = WasteBill::$tableFieldList;
        $tableData              = WasteBill::setDataByTableField($data,$fieldList);
        $tableDataCheckResult   = WasteBill::checkDataByTableField($tableData,$fieldList);
        if($tableDataCheckResult !== true)
        {
            xThrow(ERR_PARAMETER, $tableDataCheckResult);
        }
        $editId                 = WasteBill::editData($tableData,'id');
        if(!$editId)
        {
            xThrow(ERR_UNKNOWN,'添加失败');
        }
        return ['message'=>'添加成功'];
    }

}